package test;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class FlashBoom {

	// private Vector< Image > images;
	static BufferedImage[] images;
	static int NUM = 11;
	protected static int width;
	protected static int height;

	private double currentFps = 0;
	private static int maxFps = 0;
	double fps = 0.30;
	int x;
	int y;
	static {
		if (images == null) {
			maxFps = NUM - 1;
			images = new BufferedImage[NUM];
			for (int i = 0; i < NUM; i++) {
				try {

					String str = null;
						str = FlashBoom.class.getClassLoader()
								.getResource("boom/" + "boom" + i + ".png")
								.getPath();
					File file = new File(str);
					if (file.exists() && file.isFile()) {
						images[i] = ImageIO.read(new FileInputStream(file));

						if (images[i] != null) {
							System.out.println("Loading->" + str + "..OK!");
							if (i == 0) {
								width = images[i].getWidth() / 4;
								height = images[i].getHeight() / 4;
							}
							
						} else {
							System.out.println("Loading " + str + "..faill!");
						}
					}
				} catch (IOException ioe) {
					System.out.println("Loading...faill!");
					ioe.printStackTrace();
				}
			}
		}
	}

	public FlashBoom(int x, int y) {

		currentFps = 0;
		this.x = x- width / 2;
		this.y = y-height/2;
	}

	public void paint(Graphics g) {
		if (currentFps >= maxFps) {
			fps = 0;
			currentFps = -1;
		}
		if (currentFps != -1) {
			g.drawImage(images[(int) (currentFps += fps)], x , y,
					width, height, null);
//			g.drawLine(this.x+width/2, 0,this.x+width/2, 700);
//			g.drawLine(0,this.y+height/2, 700, this.y+height/2);
//			g.drawRect(x,y,width,height);
		}

	}

	public Image getImg() {

		return images[(int) (currentFps += fps)];
	}
}
